REPOROOT=../../..
# Use make help, to see the available rules
include $(REPOROOT)/transforms/.make.cicd.targets

#
# This is intended to be included across the Makefiles provided within
# a given transform's directory tree,  so must use compatible syntax.
#
################################################################################
# This defines the name of the transform and is used to match against
# expected files and is used to define the transform's image name. 
TRANSFORM_NAME=$(shell basename `pwd`)

################################################################################

# Developers should adapt this according to their implementation if they do not follow the proposed template
# We continue to refine this. The current settings are based on current template
TRANSFORM_PYTHON_SRC="-m dpk_$(TRANSFORM_NAME).runtime"
TRANSFORM_RAY_SRC="-m dpk_$(TRANSFORM_NAME).ray.runtime"


OS := $(shell uname -s)
venv::	.defaults.create-venv
	source venv/bin/activate && $(PIP) install -e $(REPOROOT)/data-processing-lib[ray,spark]
	source venv/bin/activate && $(PIP) install -e $(REPOROOT)/data-connector-lib
	if [ -e requirements.txt ]; then \
		source venv/bin/activate && $(PIP) install -r  requirements.txt; \
	fi; 
ifeq ($(OS),Darwin)
	@./install_clamd_mac.sh
	@echo -e "if [ ! -e /var/run/clamav/clamd.ctl ]; then\n    clamd --config-file=\$$(brew --prefix)/etc/clamav/clamd.conf\nfi" >> ./venv/bin/activate
	@sed -i '' -e "s#deactivate () {#deactivate () {\nps aux | grep \"[c]lamd --config-file=\$$(brew --prefix)/etc/clamav/clamd.conf\" | awk '{print \$$2}' | xargs kill\nrm -f /var/run/clamav/clamd.ctl#" ./venv/bin/activate
else
	@sudo mkdir -p /var/run/clamav && sudo chmod 777 /var/run/clamav
	@$(DOCKER) build --target clamav-local -f Dockerfile.python -t clamav-local:latest .
	@echo -e "$(DOCKER) run -d --name clamav -v /var/run/clamav:/var/run/clamav clamav-local:latest" >> ./venv/bin/activate
	@echo -e "retry=0\nuntil python -c \"import clamd; clamd.ClamdUnixSocket().ping()\" > /dev/null 2>&1; do\n    sleep 3\n    retry=\$$((retry+1))\n    if [ \$$retry -gt 20 ]; then break; fi\ndone" >> ./venv/bin/activate
	@sed -i -e "s/deactivate () {/deactivate () {\nfor c in \`$(DOCKER) ps -a | grep clamav | cut -f1 -d ' '\`; do $(DOCKER) rm -f -v \$$c; done/" ./venv/bin/activate
endif


run-cli-sample:
	make venv
	source venv/bin/activate && \
	$(PYTHON) -m dpk_malware.runtime \
             --data_local_config "{ 'input_folder' : 'test-data/input', 'output_folder' : 'output'}"  
 
